package com.fuxian.action;

import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fuxian.model.News;
import com.fuxian.service.NewsService;
import com.fuxian.util.DateUtile;
import com.fuxian.util.ReturnMapUtil;

@Controller
@Scope("prototype")
public class NewsAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(NewsAction.class);

	@Resource
	private NewsService newsService;

	/* =================================== */
	private int news_id;
	private String title;
	private String author;
	private String content;
	private int rows;
	private int page;

	public void setNews_id(int newsId) {
		news_id = newsId;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	/* =================================== */

	public String getNewsList() {
		returnMap = new HashMap<String, Object>();
		
		page = ((page == 0) ? 1 : page);
		rows = ((rows == 0) ? 1 : rows);
		int totalCount = newsService.selectCount();
		int max_page = (totalCount + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page);
		returnMap.put("total", totalCount);
		List<News> newsList = newsService.selectAllNews(rows * (page - 1),
				rows);		
		returnMap.put("rows", newsList);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功!");
		return SUCCESS;
	}

	public String getNewsInfo() {
		returnMap = new HashMap<String, Object>();
		News news = newsService.selectNewsById(news_id);
		if (news != null) {
			returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功!");
			returnMap.put("title", news.getTitle());
			returnMap.put("author", news.getAuthor());
			returnMap.put("create_time",DateUtile.timeStampToString(news.getCreate_time()));
			returnMap.put("content", news.getContent());
		}else{
			returnMap = ReturnMapUtil.setReturnHeader(returnMap,false, "查无此消息！");
		}
		return SUCCESS;
	}

	public String addNews() {
		returnMap = new HashMap<String, Object>();
		News news = new News();
		news.setTitle(title);
		news.setAuthor(author);
		news.setCreate_time(DateUtile.getTimestamp());
		news.setCreate_user((String) session.get("user_name"));
		news.setContent(content);
		newsService.addNews(news);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功!");
		return SUCCESS;
	}

	public String deleteNews() {
		returnMap = new HashMap<String, Object>();
		newsService.deleteNewsById(news_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功!");
		return SUCCESS;
	}

	public String updateNews() {
		returnMap = new HashMap<String, Object>();
		News news = new News();
		news.setNews_id(news_id);
		news.setTitle(title);
		news.setAuthor(author);
		news.setContent(content);
		newsService.modifyNews(news);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功!");
		return SUCCESS;
	}
}
